package com.genius.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONUtil;
import com.genius.annotation.PreFuncAuth;
import com.genius.core.JSONResult;
import com.genius.service.DictDataService;
import com.genius.system.bo.*;
import com.genius.system.pojo.DictData;
import com.genius.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(value = "字典编码管理接口", tags = {"用于字典编码的接口"})
@RequestMapping("dictData")
public class DictDataController {

    @Autowired
    private DictDataService dictDataService;

    @PostMapping("/selectDictDataByDictId")
    @ApiOperation(value = "查询字典编码列表（根据字典ID查询）",notes = "根据字典ID查询字典编码列表", httpMethod = "POST")
    @PreFuncAuth("SYS:DICT:SELECT")
    public JSONResult selectDictDataByDictId(@Valid @RequestBody SelectDictByIdBO selectDictByIdBO) {
        List<DictData> dictDataList = dictDataService.selectDictDataByDictId(selectDictByIdBO.getDictId());
        Map<String, Object> result = new HashMap<>();
        result.put("list",dictDataList);
        return JSONResult.ok(result);
    }

    @PostMapping("/selectDictDataById")
    @ApiOperation(value = "查询字典编码（根据字典编码ID查询）",notes = "根据字典编码ID查询字典编码", httpMethod = "POST")
    @PreFuncAuth("SYS:DICT:SELECT")
    public JSONResult selectDictDataById(@Valid @RequestBody SelectDictDataByIdBO selectDictDataByIdBO) {
        DictData dictData = dictDataService.selectDictDataById(selectDictDataByIdBO.getDictDataId());
        Map<String, Object> result = new HashMap<>();
        result.put("dictData",dictData);
        return JSONResult.ok(result);
    }

    @PostMapping("/insertDictData")
    @ApiOperation(value = "添加字典编码",notes = "添加字典编码", httpMethod = "POST")
    @PreFuncAuth("SYS:DICT:INSERT")
    public JSONResult insertDictData(@Valid @RequestBody InsertDictDataBO insertDictDataBO) {
        DictData dictData = JSONUtil.parse(insertDictDataBO).toBean(DictData.class);
        Long dictDataId = IdUtil.getSnowflake().nextId();
        dictData.setDictDataId(dictDataId);
        dictData.setCreateBy(SecurityUtils.getUsername());
        int rows = dictDataService.insertDictData(dictData);
        Map<String,Object> result = new HashMap<>();
        result.put("rows",rows);
        return JSONResult.ok(result);
    }

    @PostMapping("/updateDictData")
    @ApiOperation(value = "修改字典编码",notes = "修改字典编码", httpMethod = "POST")
    @PreFuncAuth("SYS:DICT:UPDATE")
    public JSONResult updateDictData(@Valid @RequestBody UpdateDictDataBO updateDictDataBO) {
        DictData dictData = JSONUtil.parse(updateDictDataBO).toBean(DictData.class);
        dictData.setUpdateBy(SecurityUtils.getUsername());
        int rows = dictDataService.updateDictData(dictData);
        Map<String,Object> result = new HashMap<>();
        result.put("rows",rows);
        return JSONResult.ok(result);
    }

    @PostMapping("/deleteDictDataByIds")
    @ApiOperation(value = "删除字典编码",notes = "删除字典编码", httpMethod = "POST")
    @PreFuncAuth("SYS:DICT:DELETE")
    public JSONResult deleteDictDataByIds(@Valid @RequestBody DeleteDictDataByIdsBO deleteDictDataByIdsBO) {
        int rows = dictDataService.deleteDictDataByIds(deleteDictDataByIdsBO.getDictDataIds());
        Map<String,Object> result = new HashMap<>();
        result.put("rows",rows);
        return JSONResult.ok(result);
    }
}
